#!/usr/bin/env ruby
# frozen_string_literal: true

# normalize: perform some basic normalization of each worker's output

require "json"

warn "[+] pass: normalize"

$stdin.each_line do |line|
  result = JSON.parse line, symbolize_names: true

  result[:outputs].map! do |output|
    next output if output[:result].nil?

    normalized = output[:result].each_line.map do |dec_line|
      dec_line.split.join(" ")
    end.join("\n")

    output[:result] = normalized
    output[:len] = normalized.size
    output
  end

  $stdout.puts result.to_json
end

warn "[+] pass: normalize done"
